package cn.com.git.admin.server.ctrler;

import cn.com.git.admin.server.api.feign.upms.UpmsFeignService;
import cn.com.git.admin.service.dto.SysUserDetailDTO;
import cn.com.git.admin.service.dto.SysUserListDTO;
import cn.com.git.admin.service.dto.SysUserSaveDTO;
import cn.com.git.admin.service.query.SysUserListQuery;
import cn.com.git.common.base.BaseController;
import cn.com.git.common.model.CommonStateConstant;
import cn.com.git.common.model.DataTree;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 用户管理
 * @author zhangzhenfu
 * @Description
 * @date 2020/5/18 14:20
 */
@Controller
@RequestMapping("/sys_user")
public class SysUserCtrler extends BaseController {

    @Autowired
    private UpmsFeignService upmsFeignService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("sys_user/index");
        return mv ;
    }

    @RequestMapping(value = "/list/page", method = RequestMethod.POST)
    @ResponseBody
    public ResultPage<SysUserListDTO> listPage(SysUserListQuery query) {
        ResultPage<SysUserListDTO> resultPage = upmsFeignService.sysUserListPage(query);
        return resultPage;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("sys_user/add");
        return mv ;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<SysUserSaveDTO> add(@RequestBody SysUserSaveDTO form) {
        form.getM().setState(CommonStateConstant.TRUE.getValue());
        ResultMsg<SysUserSaveDTO> resultMsg = upmsFeignService.sysUserAdd(form);
        return resultMsg;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<SysUserSaveDTO> edit(@RequestBody SysUserSaveDTO form) {
        ResultMsg<SysUserSaveDTO> resultMsg = upmsFeignService.sysUserEdit(form);
        return resultMsg;
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Integer id) {
        ModelAndView mv = new ModelAndView("sys_user/edit");
        ResultMsg<SysUserDetailDTO> resultMsg = upmsFeignService.sysUserFind(id);
        mv.addObject("dto",resultMsg.getData());
        return mv ;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg delete(@PathVariable("id") Integer id) {
        ResultMsg resultMsg = upmsFeignService.sysUserDelete(id);
        return resultMsg;
    }

    /**
     * 判断账号是否唯一
     * @param account
     * @return
     */
    @RequestMapping(value = "/account/unique", method = RequestMethod.POST)
    @ResponseBody
    public Boolean accountUnique(String account) {
        SysUserListQuery query = new SysUserListQuery();
        query.setAccount(account);
        ResultPage<SysUserListDTO> resultPage =  upmsFeignService.sysUserListPage(query);
        return CollectionUtils.isEmpty(resultPage.getRows());
    }
}
